import collections
from typing import List


class Solution:
    def sortFeatures(self, features: List[str], responses: List[str]) -> List[str]:
        count = collections.Counter()
        for response in responses:
            count += collections.Counter(set(response.split()))
        features = [[feature, i] for i, feature in enumerate(features)]
        features.sort(key=lambda x: (count[x[0]], -x[1]), reverse=True)
        return [feature for feature, i in features]


if __name__ == "__main__":
    # ["touch","cooler","lock"]
    print(Solution().sortFeatures(features=["cooler", "lock", "touch"],
                                  responses=["i like cooler cooler", "lock touch cool", "locker like touch"]))

    # ["a","aa","b","c"]
    print(Solution().sortFeatures(features=["a", "aa", "b", "c"], responses=["a", "a aa", "a a a a a", "b a"]))
